package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.PaymentMgtExcution;
import com.fortune.dto.in.paymentMgt.DistributorPaymentListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.PaymentMgtEnum;
import com.fortune.service.PaymentMgtService;
import com.fortune.util.UUIDUtil;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.paymentMgt.DistributorPaymentAddDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentEditDto;
import com.fortune.vo.paymentMgt.DistributorPayment;
import com.fortune.vo.paymentMgt.DistributorPaymentDetail;
import com.fortune.vo.paymentMgt.MOutInfoInPaymentMgt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "paymentMgt", description = "经销商回款单管理", tags = {"经销商回款单管理"})
@RestController
@RequestMapping("paymentMgt")
public class PaymentMgtController {
	@Autowired
	private PaymentMgtService service;

	@ApiOperation(value = "汇款单列表", nickname = "paymentMgtList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="approved", value="批准筛选",paramType="query",dataType="int"),
			@ApiImplicitParam(name="isDealer", value="经销商/厂商",paramType="query",dataType="int")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request, @Validated @ApiIgnore DistributorPaymentListDto dto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String mid = dUserInfo.getmId();
		String did = dUserInfo.getdId();
		PagingData pagingData = new PagingData();
		dto.setPage((dto.getPage()-1)*10);
		List<DistributorPayment> list = service.findDistributorPaymentList(dto,mid,did);
		int count = service.findDistributorPaymentListCount(dto,mid,did);
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new PaymentMgtExcution<PagingData>(pagingData, PaymentMgtEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "汇款单详情", nickname = "paymentMgtDetail", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="paymentId", value="汇款单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DistributorPaymentDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="汇款单id")String paymentId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String mid = dUserInfo.getmId();
		String did = dUserInfo.getdId();
		DistributorPaymentDetail detail = service.findDetail(mid,did,paymentId);
		return new PaymentMgtExcution<DistributorPaymentDetail>(detail,PaymentMgtEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "新增汇款单", nickname = "paymentMgtAdd", notes = "", tags={ "@石匡" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DistributorPaymentAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String mid = dUserInfo.getmId();
		String did = dUserInfo.getdId();
		String id = UUIDUtil.get32UUID();
		int num = service.add(addDto,mid,did,id);
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.ADD_SUCCESS);
		}
		return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.ADD_FALSE);
	}
	
	@ApiOperation(value = "编辑汇款单", nickname = "paymentMgtEdit", notes = "", tags={ "@石匡" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DistributorPaymentEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String mid = dUserInfo.getmId();
		String did = dUserInfo.getdId();
		int num = service.update(editDto,mid,did);
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.EDIT_SUCCESS);
		}
		return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.EDIT_FALSE);
	}
	
	@ApiOperation(value = "获取出库单列表", nickname = "paymentMgtMOutlist", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="distributorId", value="经销商id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "mOutlist",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MOutInfoInPaymentMgt>> mOutlist(@Validated @NotBlank(message="经销商id不能为空")String distributorId) {
		return null;
	}
	
	@ApiOperation(value = "删除汇款单", nickname = "paymentMgtDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="paymentId", value="汇款单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="汇款单id")String paymentId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String mid = dUserInfo.getmId();
		String did = dUserInfo.getdId();
		int num = service.deletePayment(mid,did,paymentId);
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.DELETE_SUCCESS);
		}else{
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.DELETE_FALSE);
		}
	}
}
